﻿namespace Neo4jConnect.Test
{
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Neo4jConnect.Plugins;

    [TestClass]
    public class NodeTest : TestBase
    {        
        [TestMethod]
        public void CreateNode()
        {            
            var graphDb = new GraphDatabaseService(Neo4jUri);
            Node node = graphDb.CreateNode();
            node.Save();

            Assert.IsTrue(node.Id > 0);
            Assert.IsTrue(node.GetProperties().Count() == 0);
        }

        [TestMethod]
        public void DeleteNode()
        {
            var graphDb = new GraphDatabaseService(Neo4jUri);

            int numberNodes = graphDb.GetAllNodes().Count();
            
            Node node = graphDb.CreateNode();
            node.Save();

            Assert.AreEqual(numberNodes + 1, graphDb.GetAllNodes().Count());
            
            node.Delete();

            Assert.AreEqual(numberNodes, graphDb.GetAllNodes().Count());
        }

        [TestMethod]
        public void GetNode()
        {
            var graphDb = new GraphDatabaseService(Neo4jUri);
            Node node = graphDb.CreateNode();
            node.Save();

            ulong nodeId = node.Id;
            node = graphDb.GetNode(node.Id);

            Assert.AreEqual(node.Id, nodeId);            
        }

        [TestMethod]
        public void SetAndGetPropertiesOnNode()
        {
            var graphDb = new GraphDatabaseService(Neo4jUri);
            Node node = graphDb.CreateNode();
            node.Save();

            Assert.IsTrue(node.Id > 0);
            Assert.IsTrue(node.GetProperties().Count() == 0);

            node.SetProperty("key1", "val1");
            node.SetProperty("key2", "val2");
            node.Save();

            node = graphDb.GetNode(node.Id);

            Assert.AreEqual("val1", node.GetProperty<string>("key1"));
            Assert.AreEqual("val2", node.GetProperty<string>("key2"));
        }
    }
}
